package com.zhaowb.springcloud.gate.service;

import com.zhaowb.springcloud.api.vo.user.UserInfo;
import com.zhaowb.springcloud.gate.biz.UserSecurity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

import java.util.HashSet;
import java.util.Set;

/**
 * Created with IDEA
 *
 * @author zhaowb
 * @date 2018/11/10 12:28
 */
@Repository
public class GateUserDetailsService implements UserDetailsService {

    @Autowired
    private UserSecurity userSecurity;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        if (StringUtils.isBlank(username)) {
            throw new UsernameNotFoundException("用户名为空");
        }
        UserInfo userInfo = userSecurity.getUserByUsername(username);
        Set<GrantedAuthority> authorities = new HashSet<>();

        // （username, 用户名  userInfo.getPassword() 密码,true 是否可用, true是否过期,true 证书不过期为true,true 账户未锁定为true ,authorities)
        return new User(username, userInfo.getPassword(), true, true, true, true, authorities);
    }
}
